package oocl.ita.eordering.util;

import java.text.NumberFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.service.OrderService;
@Component
public class  GradeCalculate {
	@Autowired(required=true)
	OrderService oService;
	@Autowired(required=true)
	MerchantService mService;
	
	public OrderService getoService() {
		return oService;
	}

	public void setoService(OrderService oService) {
		this.oService = oService;

	}

	public MerchantService getmService() {
		return mService;
	}

	public void setmService(MerchantService mService) {
		this.mService = mService;
	}

	public Double gradeCalculatebyM(Merchant merchant,Order order){
		System.out.println(oService);
		List<Order>orders=oService.showOrderByMerchantId(merchant.getmId());
		int orderCount=0;
		for (Order eachorder : orders) {
			if (eachorder != null) {
				orderCount++;
			}
		}
		Double grade=(merchant.getmStark()*orderCount+100+order.getoStark())/(orderCount+10);
		NumberFormat nFormat=NumberFormat.getNumberInstance(); 
		nFormat.setMaximumFractionDigits(2);
		String douGrade = nFormat.format(grade);
		merchant.setmStark(Double.parseDouble(douGrade));
		//update DB
		mService.updateMerchant(merchant);
		return grade;
		
	}
	
}
